/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.entity;

import org.ipti.br.tag.entity.enums.Laterality;
import org.ipti.br.tag.entity.enums.MaritalStatus;
import org.ipti.br.tag.entity.enums.Sex;
import java.io.Serializable;
import java.util.Calendar;
import javax.persistence.*;
import javax.validation.constraints.Size;
import org.ipti.br.tag.util.CalendarUtil;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */
@Entity
@DiscriminatorValue("H")
public class Human extends Person implements Serializable {
    
    @Size(min=3, message="O Primeiro nome deve ter no Mínimo três letras")
    @Column(nullable=false, name="first_name", length=60)
    private String firstName;
    
    @Size(min=3, message="O Sobrenome deve ter no Mínimo três letras")
    @Column(nullable=false, name="last_name", length=60)
    private String lastName;
    
    @Enumerated(EnumType.ORDINAL)
    private Sex sex;
    
    @Enumerated(EnumType.ORDINAL)
    private Laterality laterality;
    
    @Enumerated(EnumType.ORDINAL)
    @Column(name="marital_status")
    private MaritalStatus maritalStatus;
    
    @ManyToOne
    @JoinColumn(name="ethnicity_id")
    private Ethnicity ethnicity;
    
    @ManyToOne(cascade= {
        CascadeType.REFRESH, CascadeType.MERGE, CascadeType.DETACH})
    @JoinColumn(name="father_id")
    private Human father;
    
    @ManyToOne(cascade= {
        CascadeType.REFRESH, CascadeType.MERGE, CascadeType.DETACH})
    @JoinColumn(name="mother_id")
    private Human mother;
    
    @ManyToOne(cascade= {
        CascadeType.REFRESH, CascadeType.MERGE, CascadeType.DETACH})
    @JoinColumn(name="sponsor_id")
    private Human sponsor;
    
    public Human() {
        super();
    }

    public Human(String firstName, String lastName, String email, String phone, String celphone, Address address, Calendar birthday, Nationality nationality, Sex sex, Laterality laterality, MaritalStatus maritalStatus, Ethnicity ethnicity,  Human father, Human mother, Human sponsor) {
        super(email, phone, celphone, address, birthday, nationality);
        this.firstName = firstName;
        this.lastName = lastName;
        this.sex = sex;
        this.laterality = laterality;        
        this.maritalStatus = maritalStatus;
        this.ethnicity = ethnicity;
        this.father = father;
        this.mother = mother;
        this.sponsor = sponsor;
    }

    @Override
    public void generateHash() {
        if(getId() == null) {
            String hash = "" + this.getFirstName() + this.getLastName() + this.getSex()
                    + ((this.getAddress() != null) ? this.getAddress().getId() : null)
                    + ((this.getBirthday() != null) ? CalendarUtil.getFormatedDate(this.getBirthday()) : null);
            this.setId(Long.valueOf(EncryptionUtil.encrypt(hash).hashCode()));
        }
    }
    
    public short getAge() {
        Calendar c = Calendar.getInstance();
        return (short) (c.get(Calendar.YEAR) - getBirthday().get(Calendar.YEAR));
    }
    
    public String getName() {
        return firstName+" "+lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Ethnicity getEthnicity() {
        return ethnicity;
    }

    public void setEthnicity(Ethnicity ethnicity) {
        this.ethnicity = ethnicity;
    }

    public Human getFather() {
        return father;
    }

    public void setFather(Human father) {
        this.father = father;
    }

    public Laterality getLaterality() {
        return laterality;
    }

    public void setLaterality(Laterality laterality) {
        this.laterality = laterality;
    }

    public MaritalStatus getMaritalStatus() {
        return maritalStatus;
    }

    public void setMaritalStatus(MaritalStatus maritalStatus) {
        this.maritalStatus = maritalStatus;
    }

    public Human getMother() {
        return mother;
    }

    public void setMother(Human mother) {
        this.mother = mother;
    }

    public Sex getSex() {
        return sex;
    }

    public void setSex(Sex sex) {
        this.sex = sex;
    }

    public Human getSponsor() {
        return sponsor;
    }

    public void setSponsor(Human sponsor) {
        this.sponsor = sponsor;
    }
    
    @Override
    public String toString() {
        return this.getFirstName()+" "+this.getLastName();
    }  
}
